package com.dyz.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dyz.domain.SysRole;
import com.dyz.mapper.SysRoleMapper;
import com.dyz.model.Result;
import com.dyz.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author dyz
 * @since 2025-04-19
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Override
    public boolean isSuperAdmin(Long userId) {
       String code = sysRoleMapper.getUserRole(userId);
       if(!StringUtils.isEmpty(code)&&code.equals("ROLE_ADMIN")){
           return true;
       }
        return false;
    }

    @Override
    public Page<SysRole> findRoles(Page<SysRole> page, String name) {
        return page(page,new  LambdaQueryWrapper<SysRole>().like(
                !StringUtils.isEmpty(name),
                SysRole::getName,name
        ));
    }

    @Override
    public Result<?> removeRolesByIds(String[] ids) {
        if(ids==null||ids.length==0){
            return Result.fail("请选择要删除的数据");
        }
        if(removeByIds(Arrays.asList(ids))){
            return Result.success("删除成功");
        }else{
            return Result.fail("删除失败");
        }
    }
}
